//
//  UIViewController+KooNavBar.m
//  TestNav
//
//  Created by 崔宝印 on 2018/2/28.
//  Copyright © 2018年 Koolearn. All rights reserved.
//

#import "UIViewController+KooNavBar.h"
#import <objc/runtime.h>

#define KooDefaultBgColor [UIColor whiteColor]
#define KooDefaultItemColor [UIColor blackColor]
#define KooDefaultTitleColor [UIColor yellowColor]

static const char *kooNavBarAlphaKey = "kooNavBarAlphaKey";
static const char *kooNavBarTintColorKey = "kooNavBarTintColorKey";
static const char *kooNavTintColorKey = "kooNavTintColorKey";
static const char *kooNavTitileColorKey = "kooNavTitileColorKey";

@implementation UIViewController (KooNavBar)

- (void)setKooNavBarAlpha:(CGFloat)kooNavBarAlpha
{
    objc_setAssociatedObject(self, kooNavBarAlphaKey, @(kooNavBarAlpha), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self.navigationController setKooNavBarAlpha:kooNavBarAlpha];
}

- (CGFloat)kooNavBarAlpha
{
    id alpha = objc_getAssociatedObject(self, kooNavBarAlphaKey);
    
    if (alpha == NULL)
    {
        return 1.0;
    }
    else
    {
        return [alpha floatValue];
    }
}

- (void)setKooNavBarTintColor:(UIColor *)kooNavBarTintColor
{
    objc_setAssociatedObject(self, kooNavBarTintColorKey, kooNavBarTintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.navigationController.navigationBar.barTintColor = kooNavBarTintColor;
}

- (UIColor *)kooNavBarTintColor
{
    UIColor *color = objc_getAssociatedObject(self, kooNavBarTintColorKey);
    
    if (color == NULL)
    {
        return KooDefaultBgColor;
//        return self.navigationController.navigationBar.barStyle == UIBarStyleDefault ? [UIColor whiteColor] : [UIColor blackColor];
    }
    else
    {
        return color;
    }
}

- (void)setKooNavTintColor:(UIColor *)kooNavTintColor
{
    objc_setAssociatedObject(self, kooNavTintColorKey, kooNavTintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.navigationController.navigationBar.tintColor = kooNavTintColor;
}

- (UIColor *)kooNavTintColor
{
    UIColor *color = objc_getAssociatedObject(self, kooNavTintColorKey);
    if (color == NULL)
    {
        return KooDefaultItemColor;
//        return self.navigationController.navigationBar.barStyle == UIBarStyleDefault ? [UIColor whiteColor] : [UIColor blackColor];
    }
    else
    {
        return color;
    }
}

- (void)setKooNavTitileColor:(UIColor *)kooNavTitileColor
{
    objc_setAssociatedObject(self, kooNavTitileColorKey, kooNavTitileColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:kooNavTitileColor}];
}

- (UIColor *)kooNavTitileColor
{
    UIColor *color = objc_getAssociatedObject(self, kooNavTitileColorKey);
    if (color == NULL)
    {
        return KooDefaultTitleColor;
//        return self.navigationController.navigationBar.barStyle == UIBarStyleDefault ? [UIColor whiteColor] : [UIColor blackColor];
    }
    else
    {
        return color;
    }
}
@end
